package com.hwadee.caixiang.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hwadee.caixiang.entity.User;
import com.hwadee.caixiang.service.RegisterService;
//import com.hwadee.caixiang.service.UserService;

@Controller
public class RegisterController {

	@Autowired
	private RegisterService registerService;
	
	
	@GetMapping("/check")
	public @ResponseBody int check(String username) {
		if(username==""||username==null){ //用户名为空
			int result ;
			result=-1;
			return result;
		}else{
			int existUser = registerService.findByUserName(username);
			
			if(existUser == 0){
				// 用户名可以使用的
				int result;
				//result.put("msg","true");
				result = 1;
				return result;
			}else{
				// 用户名已经存在
				int result = 2;
				return result;
			}
			}
	}
	
//	@GetMapping("/checkUserName")
//	public @ResponseBody int checkUserName(String username) {
//		
//	}
//	public @ResponseBody int check(String username) {
//		
//		int result = registerService.findByUserName(username);
//		
//		return result;
//	}
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String get() {
		return "register";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String post(User user) {
		
		//System.out.println(user.getAddr()!=null&&user.getAddr()!=""&&user.getPassword()!=null&&user.getPassword()!=""&&user.getEmail()!=null&&user.getEmail()!=""&&user.getUsername()!=null&&user.getUsername()!=""&&user.getPhone()!=null&&user.getPhone()!="");
		
		
		if(user.getAddress()!=null&&user.getAddress()!=""&&user.getPassword()!=null&&user.getPassword()!=""&&user.getEmail()!=null&&user.getEmail()!=""&&user.getUsername()!=null&&user.getUsername()!=""&&user.getPhone()!=null&&user.getPhone()!="")
		{
			int rows = registerService.addUser(user);
			return "redirect:/login.jsp";
		}
		
//		System.out.println("sssssssss"+user.getUsername());
//		System.out.println("sssssssss"+user.toString());
		//forward
//		if(user.getUsername()!=null)
//		{
//			return "redirect:/login.jsp";
//		}
//		else
			return "register";
		
	}
	@GetMapping("/regist")
	public String get1() {
		
		return "register";
	}
	
}
